package com.lingchen.controller;

import com.lingchen.entity.User;
import com.lingchen.enums.MenuEnum;
import com.lingchen.nto.Menu;
import com.lingchen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Author    lingchen
 * Email     838878458@qq.com
 * Time      2018/2/26
 * Function  用户管理
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String selectAllInfo(User user, Model model) {
        User dataUser = userService.selectUser(user);

        if (dataUser != null) {
            List<Menu> list = userService.getMenu();
            model.addAttribute("list", list);
            //如果超级管理员 可以添加题库
            if (dataUser.getAuthority() == 0) {
                model.addAttribute("choice", new Menu(MenuEnum.MENU_CHOICE, list.get(0).getCourses()));
            }
            return "admin";
        } else {
            return "index";
        }
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(User user, Model model) {
        if (userService.insertUser(user) == 1) {
            model.addAttribute("list", userService.getMenu());
            return "admin";
        } else {
            return "register";
        }
    }

    @RequestMapping(value = "/registerJsp", method = RequestMethod.GET)
    public String registerJsp() {
        return "register";
    }

    @RequestMapping(value = "/show1Jsp", method = RequestMethod.GET)
    public String show1Jsp() {
        return "show1";
    }
    @RequestMapping(value = "/show2Jsp", method = RequestMethod.GET)
    public String show2Jsp() {
        return "show2";
    }

}
